package com.example.mobile_logic;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;

import com.example.mobile_BillAdapterMoudle.BillTempGeter;
import com.example.mobile_function.CommWithWCF;
import com.example.mobile_moudle.XModeRtn;
import com.example.mobile_moudle.XModeSnd;
import com.google.gson.Gson;

public class CommLogic {

	private String Message;
	private Thread _CommThread;
	private boolean _IsUsing = false;

	private BillTempGeter _BillTempGeter;
	public void setClassType(BillTempGeter billTempGeter) {
		this._BillTempGeter = billTempGeter;
	}

	private String _UID;

	public void setUID(String UID) {
		this._UID = UID;
	}

	private Handler _Handle;

	public void setHandle(Handler handler) {
		this._Handle = handler;
	}

	private Gson _gson = new Gson();

	public void CommThread() {
		_CommThread = new Thread(runnable_CommRunnable);
		_CommThread.setDaemon(true);
		_CommThread.start();
	}

	Runnable runnable_CommRunnable = new Runnable() {
		@Override
		public void run() {
			Message msg = new Message();
			try {
				if (!_IsUsing) {
					
					_IsUsing = true;
					CommWithWCF ser = new CommWithWCF();
					XModeSnd Value = new XModeSnd();
					
					Value.setFile("X01");
					Value.setMethod("GetBillTemp");
					Value.setUID(_UID);
					BillTempGeter geter = new BillTempGeter();
					geter.setClassType(_BillTempGeter.getClassType());
					Value.setJsonObject(_gson.toJson(geter));

					XModeRtn ValueRtn = ser.Code(Value,"");
					msg.obj = ValueRtn;
					msg.what = 4;
					_Handle.sendMessage(msg);
					_IsUsing = false;
				}

			} catch (Exception e) {
				msg.what = 1;
				Bundle bundle = new Bundle();
				bundle.putString("message", e.getMessage());
				msg.setData(bundle);
				_Handle.sendMessage(msg);
				_IsUsing = false;
			}
		}
	};

}
